package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.BAnjuan_af;
import com.ruoyi.system.domain.BFile_af;
import com.ruoyi.system.domain.BEMbpz;
import com.ruoyi.system.mapper.BAnjuan_afMapper;
import com.ruoyi.system.mapper.BFile_afMapper;
import com.ruoyi.system.mapper.BEMbpzMapper;
import com.ruoyi.system.mapper.BFile_afMapper;
import com.ruoyi.system.service.IBAnjuan_afService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 电子案卷新Service业务层处理
 *
 * @author guangpipi
 * @date 2023-09-21
 */
@Service
public class BAnjuan_afServiceImpl implements IBAnjuan_afService
{
    @Autowired
    private BAnjuan_afMapper bEAnjuanMapper;
    @Autowired
    private BFile_afMapper beFileMapper;
    @Autowired
    private BEMbpzMapper beMbpzMapper;

    /**
     * 查询电子案卷新
     *
     * @param id 电子案卷新主键
     * @return 电子案卷新
     */
    @Override
    public BAnjuan_af selectBEAnjuanById(String id)
    {
        return bEAnjuanMapper.selectBEAnjuanById(id);
    }

    /**
     * 查询电子案卷新列表
     *
     * @param bEAnjuan 电子案卷新
     * @return 电子案卷新
     */
    @Override
    public List<BAnjuan_af> selectBEAnjuanList(BAnjuan_af bEAnjuan)
    {
        return bEAnjuanMapper.selectBEAnjuanList(bEAnjuan);
    }

    /**
     * 新增电子案卷新
     *
     * @param bEAnjuan 电子案卷新
     * @return 结果
     */
    @Override
    public int insertBEAnjuan(BAnjuan_af bEAnjuan)
    {
        return bEAnjuanMapper.insertBEAnjuan(bEAnjuan);
    }

    /**
     *
     * 完整性检测
     *
     */
    public String integrityTesting(BAnjuan_af bEAnjuan){
        String anjuanId = bEAnjuan.getId();
        BFile_af beFile = new BFile_af();
        String res = "缺少";
        beFile.setEaid(anjuanId);
        List<BFile_af> beFileList = beFileMapper.selectBEFileList(beFile);
        List<BEMbpz> beMbpzList = beMbpzMapper.getAllBEMbpz();

        List<String> mbpzList = new ArrayList<>();
        List<String> fileList = new ArrayList<>();

        for (BEMbpz beMbpz : beMbpzList){
            if(beMbpz.getSJBH() == 2){
                mbpzList.add(beMbpz.getFLH());
            }
        }

        for (BFile_af beFile1 : beFileList){
            if(beFile1.getWenJianName() != null){
                fileList.add(beFile1.getWenJianName());
            }
        }

        for (String name : mbpzList){
            if(!fileList.contains(name)){
                res = res + name + "、";
            }
        }

        if(res.compareTo("缺少") == 0){
            return "文件完整，可通过！";
        }else{
            return "文件有缺失，请及时补全！";
        }
    }

    /**
     *
     * 缺失文件
     *
     */
    public List<String> missingFiles(BAnjuan_af bEAnjuan){
        String anjuanId = bEAnjuan.getId();
        BFile_af beFile = new BFile_af();
        List<String> res = new ArrayList<>();
        beFile.setEaid(anjuanId);
        List<BFile_af> beFileList = beFileMapper.selectBEFileList(beFile);
        List<BEMbpz> beMbpzList = beMbpzMapper.getAllBEMbpz();

        List<String> mbpzList = new ArrayList<>();
        List<String> fileList = new ArrayList<>();

        for (BEMbpz beMbpz : beMbpzList){
            if(beMbpz.getSJBH() == 2){
                mbpzList.add(beMbpz.getFLH());
            }
        }

        for (BFile_af beFile1 : beFileList){
            if(beFile1.getWenJianName() != null){
                fileList.add(beFile1.getWenJianName());
            }
        }

        Collections.sort(mbpzList);

        for (String name : mbpzList){
            if(!fileList.contains(name)){
                res.add(name);
            }
        }

        return res;
    }




    /**
     * 修改电子案卷新
     *
     * @param bEAnjuan 电子案卷新
     * @return 结果
     */
    @Override
    public int updateBEAnjuan(BAnjuan_af bEAnjuan)
    {
        return bEAnjuanMapper.updateBEAnjuan(bEAnjuan);
    }

    /**
     * 批量删除电子案卷新
     *
     * @param ids 需要删除的电子案卷新主键
     * @return 结果
     */
    @Override
    public int deleteBEAnjuanByIds(String[] ids)
    {
        return bEAnjuanMapper.deleteBEAnjuanByIds(ids);
    }

    /**
     * 删除电子案卷新信息
     *
     * @param id 电子案卷新主键
     * @return 结果
     */
    @Override
    public int deleteBEAnjuanById(String id)
    {
        return bEAnjuanMapper.deleteBEAnjuanById(id);
    }
}
